package com.allen.dao;

import com.allen.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoginDao {
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    public boolean login(String username, String password) {
        connection = JdbcUtils.getConnect();
        String sql = "SELECT * FROM user WHERE username='" + username + "' and password='" + password + "'";
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public void updateTime(String username, String password) {
        connection = JdbcUtils.getConnect();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String time = fmt.format(new Date());
        String sql = "UPDATE user SET logindate = '"+time+"' WHERE username='" + username + "' and password='" + password + "'";
        try {
            statement = connection.createStatement();
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
